// IrregularShape class - IrregularShape element for scene - draws and manages a IrregularShape in the scene
// Remark - boundaries are not done... so redrawing will not happen for this shape correctly
// the invalidation rules must be computed in the future
IrregularShape.prototype = new Shape();  
IrregularShape.prototype.constructor=IrregularShape;
function IrregularShape(points){	
	this.points = points;
	this.updateBoundary = function(){
		var points = this.points;
		if(!points)return;
		var minx,miny,maxx,maxy;
		for(var i=0;i<points.length;i++){
			var p = points[i];
			if(!minx || minx>p.x) minx = p.x;
			if(!miny || miny>p.y) miny = p.y;
			if(!maxx || maxx<p.x) maxx = p.x;
			if(!maxy || maxy<p.y) maxy = p.y;
		}
				
		this.boundary = {x:minx-this.strokeWidth,y:miny-this.strokeWidth,w:maxx-minx+this.strokeWidth*2,h:maxy-miny+this.strokeWidth*2};
	}
	this.updateBoundary();
		
	this.draw = function(ctx){
		if(this.points && this.points.length>=2){			
			ctx.beginPath();
			ctx.moveTo(this.points[0].x, this.points[0].y);
			for(var i=1;i<this.points.length;i++){
				var point = this.points[i];
				ctx.lineTo(point.x,point.y);
			}
			ctx.lineTo(this.points[0].x,this.points[0].y);
			ctx.closePath();			
			this.executeDraw(ctx);
		}
	}
	
	// checks if the shape intersects the specified boundary
	this.intersectsBoundary = function(scene, boundary){
		if(!scene)return false;
		var ctx = scene.context;
		
		ctx.beginPath();
		ctx.moveTo(this.points[0].x, this.points[0].y);
		for(var i=1;i<this.points.length;i++){
			var point = this.points[i];
			ctx.lineTo(point.x,point.y);
		}
		ctx.lineTo(this.points[0].x,this.points[0].y);
		
		var inPath = ctx.isPointInPath(boundary.x,boundary.y);
		if(!inPath) inPath = ctx.isPointInPath(boundary.x,boundary.y+boundary.h);
		if(!inPath) inPath = ctx.isPointInPath(boundary.x+boundary.w,boundary.y);
		if(!inPath) inPath = ctx.isPointInPath(boundary.x+boundary.w,boundary.y+boundary.h);
		if(!inPath) inPath = ctx.isPointInPath(boundary.x+boundary.w/2,boundary.y+boundary.h/2);
		
		return inPath;
		//return scene.domUtils.boundariesIntersect(this.boundary, boundary);
	}	
}
